package com.zqh.productquery.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.clients.ProductImgQueryClient;
import com.zqh.myfmmall.clients.ProductSkuQueryClient;
import com.zqh.myfmmall.entity.Product;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.vo.ProductVo;
import com.zqh.productquery.dao.ProductDao;
import com.zqh.productquery.service.ProductQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("productQueryService")
public class ProductQueryServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductQueryService{
    @Autowired
    private ProductImgQueryClient productImgQueryClient;
    @Autowired
    private ProductSkuQueryClient productSkuQueryClient;

    @Override
    public Result rootCategoryHighestSelling(String rootCategoryId) {
        Page<Product> productPage = new Page<>(1,6);
        query().eq("root_category_id",rootCategoryId).orderByDesc("sold_num").page(productPage);
        return Result.success(transToVo(productPage.getRecords()));
    }

    @Override
    public Result latestProduct(String categoryId) {
        Page<Product> productPage = new Page<>(1, 6);
        query().eq("category_id",categoryId).orderByDesc("create_time").page(productPage);
        List<Product> products = productPage.getRecords();
        return Result.success(transToVo(products));
    }

    @Override
    public Result highestSellingProduct(String categoryId) {
        Page<Product> productPage = new Page<>();
        query().eq("category_id",categoryId).orderByDesc("sold_num").page(productPage);
        return Result.success(transToVo(productPage.getRecords()));
    }

    public List<ProductVo> transToVo(List<Product> products){
        ArrayList<ProductVo> productVos = new ArrayList<>();
        List<String> productIds = products.stream().map(p -> p.getProductId()).collect(Collectors.toList());
        Result productsLowestPriceResult = productSkuQueryClient.getProductsLowestPrice(productIds);
        List<BigDecimal> lowestPrices = JSON.parseArray(JSON.toJSONString(productsLowestPriceResult.getData()), BigDecimal.class);
        Result mainImgResult = productImgQueryClient.getMainImgs(productIds);
        List<String> mainImgUrls = JSON.parseArray(JSON.toJSONString(mainImgResult.getData()), String.class);
        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            ProductVo productVo = new ProductVo(product);
            productVo.setMainImgUrl(mainImgUrls.get(i));
            productVo.setLowestPrice(lowestPrices.get(i));
            productVos.add(productVo);
        }
        return productVos;
    }
}
